// pages/couponDetail/couponDetail.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  qrcode = require('../../utils/qrcode.js'),
  timer,
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid: 0,
    rules: '',
    couponInfo: {},
    isload:!1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({ cid: options.id }, () => {
      that.getdata()
    })
  },
  getdata() {
    let { cid } = that.data
    that.setData({isload:!0})
    api.coupondetail(cid, res => {
      if (res.data.code == 0) {
        let { couponInfo, rules } = res.data.entity.data
        // let apart=0
        if (couponInfo.status == 1) {
          let timestamp = Date.parse(new Date()) / 1000
          let apart = Math.ceil((couponInfo.endTime - timestamp) / (24 * 60 * 60))
          couponInfo.apart = apart
        }
        couponInfo.starttime = util.formatDate(new Date(couponInfo.startTime * 1000))
        couponInfo.endtime = util.formatDate(new Date(couponInfo.endTime * 1000))
        couponInfo.data = util.formatDate(new Date(couponInfo.addTime * 1000))
        if (couponInfo.typeId == 6) {
          wx.getSystemInfo({
            success: (res) => {
              new qrcode({
                width: res.screenWidth * 0.515,
                height: res.screenWidth * 0.515,
                canvasId: 'canvas',
                text: couponInfo.qrCode,
                callback: res => {
                  wx.hideLoading();
                  that.getisDeal()
                }
              })
            },
          })
        }
        that.setData({ rules, couponInfo })
      }
    })
  },
  getSystemInfo() {
    wx.showLoading({
      title: '加载中',
    })
    wx.getSystemInfo({
      success: (res) => {
        new qrcode({
          width: res.screenWidth * 0.515,
          height: res.screenWidth * 0.515,
          canvasId: 'canvas',
          text: that.data.qrStr,
          callback: res => {
            wx.hideLoading();
            that.getisDeal()
          }
        })
      },
    })
  },
  getisDeal() {
    let { cid } = that.data
    timer = setInterval(function () {
      api.couponcheckstate(cid, res => {
        if (res.data.code == 0) {
          if (res.data.entity.data.status == 1) {
            wx.redirectTo({
              url: '../writeOffSuss/writeOffSuss',
            })
            clearInterval(timer);
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      })
    }, 3000)
    that.setData({ timer })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(that.data.isload){
      that.getdata()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})